<?php
require_once('mysql_table.php');

class subnivel2 extends mysql_table{

    function __construct(){
        parent::__construct();
        $this->TableName = "SUBNIVEL2";
        $this->IDField = "ID";        
        
        $this->fields['ID']  = "";
        $this->fields['ID_PRODUCTO']  = "";
        $this->fields['ID_CATEGORIA']  = "";
        $this->fields['ID_SUBCATEGORIA']  = "";
        $this->fields['ID_SUBNIVEL1']  = "";
        $this->fields['DESCRIPCION']  = "";
        $this->fields['DESCRIPCION_ADMIN']  = "";
        $this->fields['ORDEN']  = "";
        $this->fields['ID_STATUS'] = "";
        
    }
    
    /*
     * @return los activos
     * */
    function buscar(){
        
        $query = "SELECT SUBNIVEL2.*,
					SUBNIVEL1.DESCRIPCION AS NIVEL1,
					SUBNIVEL1.DESCRIPCION_ADMIN AS NIVEL1_DESC,
					SUBCATEGORIA.DESCRIPCION AS SUBCATE,
					SUBCATEGORIA.DESCRIPCION_ADMIN AS SUBCATE_DESC, 
					CATEGORIAS.DESCRIPCION AS CATEGORIA, 
					CATEGORIAS.DESCRIPCION_ADMIN AS CATEGORIA_DESC, 
					PRODUCTOS.DESCRIPCION AS PRODUCTO, 
					PRODUCTOS.DESCRIPCION_ADMIN AS PRODUCTO_DESC
					FROM SUBCATEGORIA, CATEGORIAS, PRODUCTOS, SUBNIVEL1, 						SUBNIVEL2  
					WHERE SUBNIVEL2.ID_STATUS=1 
					AND SUBNIVEL2.ID_CATEGORIA = CATEGORIAS.ID
					AND SUBNIVEL2.ID_PRODUCTO = PRODUCTOS.ID 
					AND SUBNIVEL2.ID_SUBCATEGORIA = SUBCATEGORIA.ID 
					AND SUBNIVEL2.ID_SUBNIVEL1 = SUBNIVEL1.ID 
					AND PRODUCTOS.ID_STATUS =1 
					AND CATEGORIAS.ID_STATUS = 1 
					AND SUBCATEGORIA.ID_STATUS = 1 
					AND SUBNIVEL1.ID_STATUS=1
					ORDER BY SUBNIVEL2.ORDEN ASC";
        $salida = $this->tovector_assoc_query($query);
        //print($query);
        return $salida;
            
    }
    
     /*
     * @return los del producto y categoria seleccionado
     * */
    function buscar_por($id_producto,$id_categoria, $id_subcategoria, $id_subnivel1){
        
        $query = "SELECT * FROM SUBNIVEL2 WHERE ID_STATUS=1 
        AND ID_PRODUCTO=".$id_producto." AND ID_CATEGORIA=".$id_categoria." AND ID_SUBCATEGORIA=".$id_subcategoria." AND ID_SUBNIVEL1=".$id_subnivel1. " ORDER BY SUBNIVEL2.ORDEN ASC";
        $salida = $this->tovector_assoc_query($query);
        //print($query);
        return $salida;
            
    }
    
    /*
     * @return nada si dos campos son iguales a los comparados
     * */
    function unique($Column1,$Value1,$Column2,$Value2,$Column3,$Value3){
        $query = "SELECT * FROM " . $this->TableName ." WHERE ".$Column1."=" . $Value1." AND ".$Column2."=" . $Value2." AND ".$Column3."=" . $Value3." AND ID_STATUS=1";
        $result = $this->runquery($query);
        //print_r($query);
        if ($this->SQLObject->num_rows($result) == 0) {
            return false;
        } else {
            $this->getdata($result);
            return true;
        }}
        
     /*
     * @return 1 si los inactivo todos
     * @param el campo por el cual se va a eliminar
     * @valor el id del campo
     * */
    function inactive_all($campo,$valor){
        
        $query = "UPDATE ".$this->TableName." SET ID_STATUS=0 WHERE ".$campo."=".$valor;
        if ($this->runquery($query)) {
            return 1;
        } else {
        $this->SQLObject->Error;
            return 0;
        }
            
    }    
    
}
?>
